from fastapi import APIRouter, Request,Query
from model import db
from model.user import User
from controller.rtn import Rtn
from model.ranking import get_user_list,get_area_list,get_company_list,get_test,get_my
from pydantic import BaseModel,Field

router = APIRouter()


@router.get("/test", summary="test")
async def test(
        request: Request,
        page: int = Query(1, ge=1, le=10000), 
):
    return get_test(page)


class pageReq(BaseModel):
    page:int = Field(default=1, ge=1 ,le=10000)
    limit:int = Field(default=10, ge=1 ,le=10000)

@router.post("/list", summary="用户排名")
async def list1(
        request: Request,
        pageReq:pageReq
):
    user:User = request.state.userInfo

    page = pageReq.page
    limit = pageReq.limit
    new_list,count = get_user_list(page,limit)

    my_ranking,my_points,avatar = get_my(user.id)

    return Rtn({
        "list": new_list,
        "count": count,
        "myRanking": my_ranking,
        "myPoints": my_points,
        "avatar": avatar,
    })



@router.post("/area_list", summary="地区排名")
async def area_list(
        request: Request,
        pageReq:pageReq
):
    
    user:User = request.state.userInfo
    page = pageReq.page
    limit = pageReq.limit

    new_list,count = get_area_list(page,limit)

    return Rtn({
        "list": new_list,
        "count": count,
    })


@router.post("/company_list", summary="公司排名")
async def company_list(
        request: Request,
        pageReq:pageReq
):
    page = pageReq.page
    limit = pageReq.limit

    new_list,count = get_company_list(page,limit)

    return Rtn({
        "list": new_list,
        "count": count,
    })
